package com.mk.admin.service.impl;

import com.alibaba.fastjson2.*;
import com.mk.admin.mapper.*;
import com.mk.admin.model.dto.*;
import com.mk.admin.model.entity.*;
import com.mk.admin.model.vo.*;
import com.mk.admin.service.*;
import com.mk.admin.util.*;
import io.swagger.models.auth.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;
import java.math.BigDecimal;
import com.mk.admin.util.*;

@Service
public class ChinaDrugDoseLowServiceImpl implements ChinaDrugDoseLowService {
    @Autowired
    private ChinaDrugDoseLowMapper cnDoseLowMapper;


    @Override
    public ChinaDrugDoseLowDto select(MKCheckVo vo) {
        ChinaDrugDoseLowEntity entity = JSONCopy.copy(vo, ChinaDrugDoseLowEntity.class);
        ChinaDrugDoseLowDto dto = new ChinaDrugDoseLowDto();
        vo.getOrder().forEach(item -> {
            if(item.getDosage().isEmpty()||item.getUsage().isEmpty()||item.getDosageUnit().isEmpty()) return;
            entity.setEnable(1);
            List<ChinaDrugDoseLowDto> list = cnDoseLowMapper.select(entity);
            if (list != null && list.size() == 0) {
               return;
            }
            String exRoute = list.get(0).getExRoute();
            if(exRoute!=null){
                JSONArray jsonArray = JSONArray.parseArray(exRoute);
                List<String> routeAry = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    String obj = jsonArray.getJSONObject(i).getString("exRoute");
                    routeAry.add(obj);
                }
                //用法在排除用法 跳出
                if (routeAry.contains(item.getUsage())) return;
            }
            //剂量单位相同
            if (list.get(0).getDosageUnit().equals(item.getDosageUnit())) {
                //低于预设剂量 预警
                if (Double.valueOf(item.getDosage()) < Double.valueOf(list.get(0).getDosage())) {
                    dto.setCite(list.get(0).getCite());
                    dto.setLevel(list.get(0).getLevel());
                    dto.setWay(list.get(0).getWay());
                    String str = "当前药品剂量"+item.getDosage()+""+item.getDosageUnit()+"低于规定剂量"+list.get(0).getDosage()+""+item.getDosageUnit();
                    dto.setDescribe(str);
                }
            }else {
                BigDecimal scale = UnitConverter.getUnitScale(item.getDosageUnit() ,list.get(0).getDosageUnit(), null);
                if(scale!=null){
                    BigDecimal dose = new BigDecimal(item.getDosage()).multiply(scale);
                    BigDecimal dataDose = new BigDecimal(list.get(0).getDosage());
                    if ( dose.compareTo(dataDose) <0 ) {
                        dto.setCite(list.get(0).getCite());
                        dto.setLevel(list.get(0).getLevel());
                        dto.setWay(list.get(0).getWay());
                        String str = "当前药品剂量"+item.getDosage()+""+item.getDosageUnit()+"低于规定剂量"+list.get(0).getDosage()+""+list.get(0).getDosageUnit();
                        dto.setDescribe(str);
                    }
                }
            }
        });
        return dto;
    }
}
